﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class AddNewAccount : System.Web.UI.UserControl
    {
        public int qldanhmuc;
        public int qlsanpham;
        public int qlgiaodien;
        public int qlkhachhang;
        public int qlhoadon;
        public int qlttamark;
        public int nextidaccount;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_Bank", "Bank_ID"));
            string username = txtUserAccount.Text;
            string password = txtPassword.Text;
            string fullname = txtFullname.Text;
            string email = txtEmail.Text;
            string phonenumber = txtPhone.Text;
            string address = txtAddress.Text;
            string role = ddlRole.Text;
            string filePath = null;
            if (cbxqldanhmuc.Checked == true)
            {
                qldanhmuc = 1;
            }
            else
            {
                qldanhmuc = 0;
            }
            if (cbxqlgiaodien.Checked == true)
            {
                qlgiaodien = 1;
            }
            else
            {
                qlgiaodien = 0;
            }
            if (cbxqlhoadon.Checked == true)
            {
                qlhoadon = 1;
            }
            else
            {
                qlhoadon = 0;
            }
            if (cbxqlkhachhang.Checked == true)
            {
                qlkhachhang = 1;
            }
            else
            {
                qlkhachhang = 0;
            }
            if (cbxqlsanpham.Checked == true)
            {
                qlsanpham = 1;
            }
            else
            {
                qlsanpham = 0;
            }
            if (cbxqlttAmark.Checked == true)
            {
                qlttamark = 1;
            }
            else
            {
                qlttamark = 0;
            }

            try
            {
                string password_mahoa = LIB.Converter.Encode("sha1", password);
                DataTable checkuseremail = DAL.DCollector.DBCollector.Instance.StoredProcedureForgetPassword("checkuseraccountexit", username, email);
                if (checkuseremail.Rows[0]["result"].ToString() == "0")
                {
                    if (fulImageAccount.HasFile)
                    {
                        if (LIB.Validate.CheckFileType(fulImageAccount.FileName))
                        {
                            if (role.Equals("Admin"))
                            {
                                nextidaccount = nextid + 1;
                                string iamgeaccount = fulImageAccount.FileName;
                                LIB.Validate saveimage = new LIB.Validate();
                                saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Account/"), iamgeaccount, 250, fulImageAccount.PostedFile.InputStream);
                                filePath = "/Images/Image-Account/" + fulImageAccount.FileName;
                                string value = "" + nextidaccount + ",N'" + username + "','" + password_mahoa + "','" + email + "',N'" + fullname + "','" + phonenumber + "',N'" + address + "','" + filePath + "','" + role + "',1,1,1,1,1,1";
                                List<string> values = new List<string>();
                                values.Add(value);
                                DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Account", "insertemployee", values, false);
                                //Session["UsernameAccount"] = txtUserAccount.Text;
                                Response.Write("<script>alert('Tài khoản đã được tạo...')</script>");
                            }
                            else
                            {
                                nextidaccount = nextid + 1;
                                string iamgeaccount = fulImageAccount.FileName;
                                LIB.Validate saveimage = new LIB.Validate();
                                saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Account/"), iamgeaccount, 250, fulImageAccount.PostedFile.InputStream);
                                filePath = "/Images/Image-Account/" + fulImageAccount.FileName;
                                string value = "" + nextidaccount + ",N'" + username + "','" + password_mahoa + "','" + email + "',N'" + fullname + "','" + phonenumber + "',N'" + address + "','" + filePath + "','" + role + "'," + qlsanpham + "," + qldanhmuc + "," + qlhoadon + "," + qlkhachhang + "," + qlttamark + "," + qlgiaodien + "";
                                List<string> values = new List<string>();
                                values.Add(value);
                                DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Account", "insertemployee", values, false);
                                //Session["UsernameAccount"] = txtUserAccount.Text;
                                Response.Write("<script>alert('Tài khoản đã được tạo...')</script>");
                            }
                        }
                        else
                        {
                            lblCheckFileImages.Text = "Chỉ có thể upload file(.jpg,.png,.jpeg)";
                        }
                    }
                    else
                    {
                        if (nextid == 0)
                        {
                            string value = "1,N'" + username + "','" + password_mahoa + "','" + email + "',N'" + fullname + "','" + phonenumber + "',N'" + address + "','" + filePath + "','" + role + "'," + qlsanpham + "," + qldanhmuc + "," + qlhoadon + "," + qlkhachhang + "," + qlttamark + "," + qlgiaodien + "";
                            List<string> values = new List<string>();
                            values.Add(value);
                            DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Account", "insertemployee", values, false);
                            //Session["UsernameAccount"] = txtUserAccount.Text;
                            Response.Write("<script>alert('Tài khoản đã được tạo...')</script>");
                        }
                        else
                        {
                            nextidaccount = nextid + 1;
                            string value = "" + nextidaccount + ",N'" + username + "','" + password_mahoa + "','" + email + "',N'" + fullname + "','" + phonenumber + "',N'" + address + "','" + filePath + "','" + role + "'," + qlsanpham + "," + qldanhmuc + "," + qlhoadon + "," + qlkhachhang + "," + qlttamark + "," + qlgiaodien + "";
                            List<string> values = new List<string>();
                            values.Add(value);
                            DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Account", "insertemployee", values, false);
                            //Session["UsernameAccount"] = txtUserAccount.Text;
                            Response.Write("<script>alert('Tài khoản đã được tạo...')</script>");
                        }

                    }
                }
                else
                {
                    Response.Write("<script>alert('Tên tài khoản hoặc Email đã tồn tại')</script>");
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtUserAccount.Text = "";
            txtPassword.Text = "";
            txtPhone.Text = "";
            txtFullname.Text = "";
            txtEmail.Text = "";
            txtAddress.Text = "";
            txtUserAccount.Text = "";
        }

        protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlRole.Text.Equals("Mod"))
            {
                lblqlmenu.Visible = true;
                cbxqldanhmuc.Visible = true;
                cbxqlgiaodien.Visible = true;
                cbxqlhoadon.Visible = true;
                cbxqlkhachhang.Visible = true;
                cbxqlsanpham.Visible = true;
                cbxqlttAmark.Visible = true;
            }
            else
            {
                lblqlmenu.Visible = false;
                cbxqldanhmuc.Visible = false;
                cbxqlgiaodien.Visible = false;
                cbxqlhoadon.Visible = false;
                cbxqlkhachhang.Visible = false;
                cbxqlsanpham.Visible = false;
                cbxqlttAmark.Visible = false;
            }
        }
    }
}